package com.wenhuitec.productlib.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.gzwh.weblib.bean.MenuFoodModel;
import com.wenhuitec.productlib.R;
import com.wenhuitec.productlib.entity.ProductFoods;
import com.wenhuitec.sysui.view.MenuFoodButton;

import java.util.List;

public class MenuFoodAdapter extends RecyclerView.Adapter<MenuFoodAdapter.MyViewHolder> {
    private List<ProductFoods> mList;
    private LayoutInflater inflater;
    private Context mContext;

    public interface OnUserClick {
        void onClick(String foodId, ProductFoods menuFoodModel);
    }

    public void setmList(List<ProductFoods> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    private OnUserClick onPutUserClick;
    private OnUserClick onFoodClick;
    private OnUserClick onStockClick;

    public void setOnStockClick(OnUserClick onStockClick) {
        this.onStockClick = onStockClick;
    }

    //商品点击
    public void setOnFoodClick(OnUserClick onFoodClick) {
        this.onFoodClick = onFoodClick;
    }

    //上下架点击
    public void setOnPutUserClick(OnUserClick onPutUserClick) {
        this.onPutUserClick = onPutUserClick;
    }

    public MenuFoodAdapter(Context context, List<ProductFoods> datas, OnUserClick onUserClick) {
        inflater = LayoutInflater.from(context);
        mContext = context;
        this.mList = datas;
        this.onFoodClick = onUserClick;
    }

    public void toShow(List<ProductFoods> foodModels) {
        this.mList = foodModels;
        notifyDataSetChanged();
    }

    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public MenuFoodAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_product_foods, null);
        return new MyViewHolder(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(MenuFoodAdapter.MyViewHolder holder, int position) {

        holder.tvName.setText(mList.get(position).getProductName());
        if (mList.get(position).getSkuList().size() > 0) {
            holder.tvPrice.setText(mList.get(position).getSkuList().get(0).getPrice());
        }

        holder.foodMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onFoodClick != null) {
                    onFoodClick.onClick(mList.get(position).getId(), mList.get(position));
                }
            }
        });
//        MenuFoodModel.Sku sku = mList.get(position).getSkuList().stream().min(Comparator.comparing(MenuFoodModel.Sku::getStock)).get();
//        MenuFoodModel.Sku sku = mList.get(position).getSkuList().get(0);
//        for (MenuFoodModel.Sku goo : mList.get(position).getSkuList()) {
//            if (goo.getStock() < sku.getStock()) {
//                sku = goo;
//            }
//        }
        //region 库存渲染和点击
//        holder.mStorageBut.setKuNum(sku.getStock(), () -> {
//            if (onStockClick != null) {
//                onStockClick.onClick(mList.get(position).getFoodId(), mList.get(position));
//            }
//        });
        //endregion

        //region 上下架按钮渲染和点击
//        holder.mPutBut.setPutShelves(mList.get(position).getIsShow() == 0 ? false : true, position, () -> {
//            boolean temp = mList.get(position).getIsShow() == 0 ? false : true;
//            holder.mPutBut.updatePutStatus(!temp);
//            if (onPutUserClick != null) {
//                onPutUserClick.onClick(mList.get(position).getFoodId(), mList.get(position));
//            }
//        });
        //endregion

        //region 图片渲染
        try {
//            Glide
//                    .with(mContext)
//                    .load(mList.get(position).getPic())
//                    .centerCrop()
//                    .placeholder(R.drawable.umeng_socialize_share_web)
//                    .crossFade()
//                    .into(holder.ivFood);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //endregion


    }

    //数据源的数量
    @Override
    public int getItemCount() {
        return mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivFood;
        private TextView tvName;
        private TextView tvPrice;
        private MenuFoodButton mPutBut;
        private MenuFoodButton mStorageBut;
        private LinearLayout foodMain;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivFood = (ImageView) itemView.findViewById(R.id.iv_food);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            mPutBut = (MenuFoodButton) itemView.findViewById(R.id.putBut);
//            mStorageBut = (MenuFoodButton) itemView.findViewById(R.id.storageBut);
            foodMain = (LinearLayout) itemView.findViewById(R.id.food_main);
        }
    }
}
